package com.ljb.monitorview.monitor3;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.model.GetObjectRequest;
import com.ljb.monitorview.R;
import com.ljb.utilslibrary.commUtils.AssetsDataTool;
import com.ljb.utilslibrary.commUtils.JsonKit;
import com.ljb.utilslibrary.ossutils.OssController;
import com.ljb.utilslibrary.ossutils.listener.DownLoadCBListener;

import java.io.IOException;
import java.io.InputStream;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity3 extends AppCompatActivity {
    private static final String TAG = "MainActivity3";
    private MonitorView3 mv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initUI();
        getConfig();
    }

    /**
     * 重新加载界面
     *
     * @param view
     */
    public void ReloadData(View view) {
        mv.clearData();
        getConfig();
    }

    /**
     * 获取配置数据
     */
    private void getConfig() {
        ConfigBean3 configuration = AssetsDataTool.init().readData(MainActivity3.this, "configuration", ConfigBean3.class);
        //设置配置数据
        mv.setConfig(configuration);

        getOSSData();
    }

    /**
     * 获取OSS上的位置数据
     */
    private void getOSSData() {
        OssController.getInstance(this).downloadObject(OssController.BUCKET_MONITOR_XML, "1151041334/monitor_points_18", new DownLoadCBListener() {
            @Override
            public void onSuccess(InputStream inputStream) {
                Log.e(TAG, "onSuccess: 位置资源下载成功");

                final MonitorBean3 monitorBean3 = AssetsDataTool.init().formIS(inputStream, MonitorBean3.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //设置OSS上的数据
                        mv.setOSSData(monitorBean3);
                        getRealData();
                    }
                });
            }

            @Override
            public void onFailure(GetObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
                Log.e(TAG, "onFailure: 位置资源下载失败");
            }
        });
    }

    /**
     * 获取真实数据
     */
    private void getRealData() {
        OkHttpClient client = new OkHttpClient();
         Request request = new Request.Builder().url("http://app.shukuang.cn/ZHSW_APP_INTERFACE/phone/monitor/getMonitorDatasByManageId_new.ajax?factoryId=1151041334&manageId=18&dataType=1").build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG, "onFailure: 获取真实数据失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String result = response.body().string();
                    Log.e(TAG, "onResponse: 获取真实数据成功: " + result);
                    final NotifyData3 notifyData3 = JsonKit.toObj(result, NotifyData3.class);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //设置动态刷新的数据
                            mv.notifyData(notifyData3);
                        }
                    });
                }
            }
        });
    }

    private void initUI() {
        mv = (MonitorView3) findViewById(R.id.mv);
    }
}
